﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
    using System.Security.Policy;
    using System.Xml;
    using IS.Projecto.Modelo;

    class Program
    {
        static void Main(string[] args)
        {
            //var list = new SerializableList<Curso>
            //               {
            //                   new Curso {Id = 1, Nome = "Curso 1", Escola = "ESTG", Regime = "Diurno"},
            //                   new Curso {Id = 2, Nome = "Curso 2", Escola = "ESTG", Regime = "Diurno"},
            //                   new Curso {Id = 3, Nome = "Curso 3", Escola = "ESTG", Regime = "Nocturno"},
            //                   new Curso {Id = 4, Nome = "Curso 4", Escola = "ESTG", Regime = "Diurno"}
            //               };

            var list = new SerializableCollection<Class1>
                           {
                               new Class1 {Id = 1, Dsc = "item 1"},
                               new Class1 {Id = 2, Dsc = "item 2"},
                               new Class1 {Id = 3, Dsc = "item 3"},
                               new Class1 {Id = 4, Dsc = "item 4"}
                           };

            //SerializeList(list.FirstOrDefault(), "Lista", "cursos2.xml");
            SerializeList(list, "Lista", "cursos.xml");

            //new XmlNamespaceTest().SerializeObject("xmlnamespaceteste.xml");
        }

        private static void SerializeList(object obj, string rootName, string filename)
        {
            var root = new XmlRootAttribute
                           {
                               ElementName = rootName,
                               Namespace = "http://xml.is.estg.org/schema/DataSchema"
                           };
            var type = obj.GetType();
            var serializer = new XmlSerializer(type, root);
            var filestream = new FileStream(filename, FileMode.Create);
            var writer = new MyXmlTextWriter(filestream, Encoding.UTF8);
            
            //var writer = new StreamWriter(filestream);

            serializer.Serialize(writer, obj);
            filestream.Close();
        }
    }

    public class MyXmlTextWriter : XmlTextWriter
    {
        public MyXmlTextWriter(Stream w, Encoding encoding) : base(w, encoding)
        {
        }

        public override void WriteStartAttribute(string prefix, string localName, string ns)
        {
            base.WriteStartAttribute(prefix, localName, ns);
            if (prefix == "xsi")
                base.WriteAttributeString("schemaLocation", prefix, "schema");
        }

        public override void WriteStartElement(string prefix, string localName, string ns)
        {
            base.WriteStartElement(prefix, localName, ns);
            //
            int a;
            //
        }

        public override void WriteDocType(string name, string pubid, string sysid, string subset)
        {
            base.WriteDocType(name, pubid, sysid, subset);
            //
            int a;
            //
        }

        public override void WriteQualifiedName(string localName, string ns)
        {
            base.WriteQualifiedName(localName, ns);
        }
    }
}
